from WindPy import w
import pandas as pd

w.start()




codeList3=["10002157.SH","10002158.SH","10002159.SH","10002160.SH","10002227.SH","10002164.SH","10002166.SH",]
posList3=[-1,4,5,16,-10,-1,2]
name3=['沪300ETF购3月4000','沪300ETF购3月4100','沪300ETF购3月4200','沪300ETF购3月4300','沪300ETF购3月4600','沪300ETF沽3月3800','沪300ETF沽3月4000']

codeList6=["10002229.SH","10002181.SH","10002183.SH","10002184.SH","10002185.SH","10002186.SH","10002222.SH","10002230.SH"]
posList6=[-30,-4,1,0,2,-2,-2,0]
name6=['沪300ETF购6月4600','沪300ETF沽6月3700','沪300ETF沽6月3900','沪300ETF沽6月4000','沪300ETF沽6月4100','沪300ETF沽6月4200','沪300ETF沽6月4500','沪300ETF沽6月4600']

codeList=codeList3+codeList6
posList=posList3+posList6
name=name3+name6


def getOptionDataFromWind(codeList,posList):
    raw=w.wsq(codeList, "rt_bid1,rt_ask1,rt_bid1_ivl,rt_ask1_ivl,rt_ustock_price,rt_int_value,rt_time_value,rt_delta,rt_gamma,rt_vega,rt_theta,rt_rho")
    raw2=w.wss(codeList, "sec_name")


    data=raw.Data
    data.append(codeList)
    data.append(posList)
    data.append(raw2.Data[0])
    result=pd.DataFrame(data,index=['bid1','ask1','volbid1','volask1','underlying','intrinsic','timevalue','delta','gamma','vega','theta','rho','code','pos','name'])
    return result.T
mydata=getOptionDataFromWind(codeList,posList)
mydata.sort_values(['name'])
print(mydata)
delta=(mydata['delta']*mydata['pos']).sum()
gamma=(mydata['gamma']*mydata['pos']).sum()
vega=(mydata['vega']*mydata['pos']).sum()
theta=(mydata['theta']*mydata['pos']).sum()
vol=((mydata['volbid1']+mydata['volask1'])/2*mydata['pos']).sum()/(mydata['pos'].sum())
total={'delta':delta,"gamma":gamma,"vega":vega,"theta":theta,'vol':vol}
print(total)